<?php

namespace Biz\service\advertisement\apiModule;

use Doraemon\pockets\constInterface\LinkConf;
use Doraemon\service\statSign\Basic;
use Doraemon\tools\BlockType;
use Doraemon\tools\HhzClient;
use Doraemon\tools\ImgUrl;

class CustomBanner extends BaseModule
{

    const FORMAT_HIT = 'formatPosHit';
    private bool $is_purchase_banner;
    private bool $is_home_community_top_banner;
    private bool $is_search_suggest_rank;

    public function __construct($position_id = 0, $userProfile = null)
    {
        parent::__construct($position_id, $userProfile);
        $this->is_search_suggest_rank = $this->position_id == 55;
        $this->is_purchase_banner = $this->position_id == 124;
        $this->is_home_community_top_banner = $this->position_id == 123;
    }


    /**
     * 推荐线顶部轮播api格式化输出
     * @param $hit
     * @param $uid
     * @param $userProfileExt
     * @param $statSignExt
     * @return array
     */
    public function format($hit, $uid, $userProfileExt, $statSignExt): array
    {
        $hits = $hit;
        $defaultTimes = 3;
        if (!empty($this->position_conf['options']) && !empty($this->position_conf['options']['gap'])) {
            $defaultTimes = $this->position_conf['options']['gap'];
        }
        $banner_list = [];
        $response = [
            'type' => BlockType::TYPE_CAROUSEL_BANNER,
            'banner_list' => &$banner_list,
        ];

        if ($hits) {
            $i = 1;
            $sort = array_column($hits, 'pos');
            array_multisort($sort, SORT_ASC, $hits);
            if ($this->is_search_suggest_rank) return $this->formatSearchSuggestRank($hits, $statSignExt);
            foreach ($hits as $hit) {
                $statSignExt = [Basic::EXTEND_KEY_CONFIG_ID => $hit['id']];
                if ($this->is_purchase_banner) {
                    $statSignExt = [
                        Basic::EXTEND_KEY_CONFIG_ID => $hit['id'],
                        Basic::EXTEND_KEY_POS => 'top',
                    ];
                }
                //公共信息赋值
                $id = intval($hit['id']);
                $hit['pos'] = $i;
                switch ($hit['position_id']) {
                    default:
                        $response['time'] = $defaultTimes;
                        $linkParams = $hit['link_params'];
                        $statSign = $this->statSign($hit, $statSignExt);
                        $linkParams = self::addStatSignToLink($linkParams, $statSign);
                        $banner_list[] = [
                            'id' => $id,
                            'index' => $i,
                            'banner' => empty($hit['pic_id']) ? '' : ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $hit['pic_id'], ImgUrl::IMAGE_SIZE_OP_ORI),
                            'link' => $this->generateLink($hit['link_type'], $linkParams),
                            'statSign' => $statSign,
                        ];
                        break;
                }
                $i++;
            }
        }
        return $response;
    }

    public function formatAdmListExtInfo(&$v, $map)
    {
        if ($this->is_search_suggest_rank) {
            $v['ext']['key_word'] = $map[$v['id']]['key_word'] ?? '';
            $v['ext']['sign'] = $map[$v['id']]['sign'] ?? '';
        }

    }

    public function uniquePos($v): string
    {
        return $v['pos'];
    }

    private function formatSearchSuggestRank($hit, $statSignExt): array
    {
        $response = [];

        if (empty($hit)) return $response;

        foreach ($hit as $v) {
            $response[$v['pos']] = [
                'is_ad' => empty($v['ext']['sign']) ? 0 : 1,
                'keyword' => $v['ext']['key_word'] ?? '',
                'link' => $v['link_type'] == LinkConf::LINK_APP_HTTPS ? $this->generateLink($v['link_type'], $v['link_params']) : '',
                'statSign' => $this->statSign($v, $statSignExt, [
                    'Toplist_type' => 'hotword',
                    'text' => $v['ext']['key_word'] ?? '',
                ])
            ];
        }

        return $response;
    }
}